<?php
/** 
 * 启动核心类
 * @date 2015-12-10
 * @modify date 2015-12-11
 * @author wzl
 */
class main {
    /**类名值c*/
    protected $class_val = null;
    /**方法值m*/
    protected $method_val = null;
    /**别名类a*/
    protected $alias_class = null;
    /**文件路径*/
    protected $file_path = null;
    
    public static function load(){
        return new self();
    }
    /**
     * 用于直接浏览
     */
    public function display(){
        /**返回状态值*/
        $status = 0;
        /**返回数据值*/
        $message = '';
        if($this->init()) {
            $message = str_replace(APP_PATH, '', $this->display_run($status));
            if($status !== 0) {
                echo $message;
            }
        } else {
            $status = 102;
            $message = 'm,f params not set...';
            echo $message;
        }      
    }
    /**
     * 显示display
     * @param int $status
     * @return boolean
     */
    private function display_run(&$status){
        /**调用Open/controllers里的类*/
        if($this->class_val && $this->method_val) {
            if(strpos($this->class_val, 'Controller') === false) {
                $this->class_val .= 'Controller';
            }
            //首先假定在controller下
            $class_dir = APP_PATH . 'controllers/';
            $class_file = $class_dir . $this->class_val . '.php';
            $this->file_path = $class_file;
            mylog_debug($this->file_path, 'file_path');
            if(is_file($class_file)) {
                if(!class_exists($this->class_val)) {
                    mylogger($this->class_val, '类未定义');
                    $status = 101;
                    return $this->class_val . '类未定义;' . $this->file_path;
                }
                $obj = new $this->class_val;       
                if(method_exists($obj, $this->method_val)) {                    
                    /**调用方法核心处*/
                    try{
                        call_user_func(array($obj, $this->method_val));
                        $status = 0;
                        return true;
                    }  catch (Exception $ex) {
                        $status = 102;
                        return $ex->getMessage();
                    }
                }
            } else {
                $status = 103;
                return $class_file .'不存在';
            }
        } else {
            $status = 104;
            return false;
        }
    }
    /**
     * 用于 call:run 应用 
     */
    public function run() {
        /**返回状态值*/
        $status = 0;
        /**返回数据值*/
        $message = '';
        if($this->init()) {
            $message = $this->_run($status);
            mylog_debug($message, 'run_message');
        } else {
            $status = 102;
            $message = 'm,f未设置';
            mylogger($message, $message);
        }
        $openapi = new OpenApiServer();
        if($status != 0) {
            $message = str_replace(APP_PATH, '', $message);//隐藏实际根目录
        }
        echo $openapi->setReturn($status, $message);
    }

    /**
     * 独的RUN
     * @param int $status
     * @return string
     */
    private function _run(&$status){
        /**调用Open/controllers里的类*/
        if($this->class_val && $this->method_val) {
            //首先假定在controller下
            $class_dir = APP_PATH . 'controllers/';
            $class_file = $class_dir . $this->class_val . '.php';
            $this->file_path = $class_file;
            mylog_debug($this->file_path, 'file_path');
            if(is_file($class_file)) {
                $message = $this->call_function($this->class_val, $this->method_val, $status);
            } else {
                //若不存在，再假定在models下
                $class_dir = APP_PATH . 'models/';
                $class_file = $class_dir . $this->class_val . '.php';  
                $this->file_path = $class_file;
                if(is_file($class_file)) {                        
                    $message = $this->call_function($this->class_val, $this->method_val, $status);
                } else {
                    $status = 100;
                    $file = $this->class_val . '.php';
                    if(strpos(strtolower($file), 'controller') !== false) {
                        $message = 'controllers/' . $file;
                    } 
                    elseif(strpos(strtolower($file), 'model') !== false) {
                        $message = 'models/' . $file;
                    } 
                    else {
                        $message = 'controller或model下' . $file;
                    }                        
                    $message .= '文件不存在,无法实例' . $this->class_val . '类';
                    mylogger($message, '文件不存在');
                }
            }
        }
        return $message;
    }
    
    /**
     * 调用方法
     * @param string $class 类名
     * @param string $function 方法名
     */
    private function call_function($class, $function, &$status) {
        /*判断是否有别名*/
        if($this->alias_class != null) {
            $class = $this->alias_class;
        }
        if(!class_exists($class)) {
            mylogger($class, '类未定义');
            $status = 108;
            return $class . '类未定义;' . $this->file_path;
        }
        $obj = new $class;
        mylog_debug(array('obj' => $obj, 'info' => 'class:' . $class . ',function:' . $function), 'debug_obj_class_function');
        if(method_exists($obj, $function)) {
            $oepnapi = new OpenApiServer();
            $receive_data = $oepnapi->receive();
            mylog_debug($receive_data, 'receive_data');
            if($receive_data === false) {
                $status = 109;
                return '签名错误';
            }
            /**调用方法核心处*/
            try{
                $data = call_user_func_array(array($obj, $function), $receive_data);
                mylog_debug($data, 'debug_data');
                $status = 0;
                return $data;
            }  catch (Exception $ex) {
                $status = 107;
                return $ex->getMessage();
            }
        } else {
            mylogger('class:' . $class . ',fun:' . $function, '方法名不存在');
            $status = 110;
            return $function . '方法名不存在;' . $this->file_path;
        }
    }
    /**
     * 初使$_GET
     * @return boolean
     */
    private function init() {
        mylog_debug($_GET, 'debug_init');
        if(isset($_GET['a'])) {
            $this->alias_class = trim($_GET['a']);//设置 类别名
        }
        //接受class和model 参数
        if(isset($_GET['c']) && isset($_GET['m'])) {
            $this->class_val = trim($_GET['c']);
            $this->method_val = trim($_GET['m']);
            return true;
        } else {
            $this->class_val = 'manager';
            $this->method_val = 'index';
            return true;            
        }
    }
}